package com.ls.jdbc.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.ls.jdbc.User;
import com.ls.jdbc.util.JDBCUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws Exception {

//        InputStream inputStream = Main.class.getResourceAsStream("/druid.properties");
//        Properties properties = new Properties();
//        properties.load(inputStream);
//
//        DataSource source = DruidDataSourceFactory.createDataSource(properties);
//        Connection connection = source.getConnection();
//        System.out.println(connection);

        Connection connection1 = DruidJdbcUtils.getConnection();
        System.out.println(connection1);
        PreparedStatement preparedStatement = connection1.prepareStatement("select * from user where id = ?");
        preparedStatement.setInt(1, 1);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<User> list = new ArrayList<>();
        User user = null;
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String card_id = resultSet.getString("card_id");
            user = new User();
            user.setId(id);
            user.setName(name);
            user.setCardId(card_id);
            list.add(user);
        }
        JDBCUtils.close(resultSet, preparedStatement, connection1);

        list.stream().forEach(System.out::println);
        connection1.close();

    }
}
